//
//  GoodListViewController.m
//  Menu
//
//  Created by Lynn-Shin on 15/7/3.
//  Copyright (c) 2015年 lijiangang. All rights reserved.
//

#import "GoodListViewController.h"

@interface GoodListViewController ()

@end

@implementation GoodListViewController

-(void)dealloc{
    
    [_goodListCollectV release];
    [_goodListArray release];
    [_HUD release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(170*WIDTH, 120*HEIGHT);
    flowLayout.sectionInset = UIEdgeInsetsMake(10*HEIGHT, 10*WIDTH, 10*HEIGHT, 10*WIDTH);
    flowLayout.minimumInteritemSpacing = 10*HEIGHT;
    flowLayout.minimumLineSpacing = 10*HEIGHT;
    
    self.goodListCollectV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, 375*WIDTH, 667*HEIGHT-64-49) collectionViewLayout:flowLayout];
    self.goodListCollectV.backgroundColor = [UIColor colorWithRed:230/255.0 green:200/255.0 blue:110/255.0 alpha:1];
    [self.view addSubview:self.goodListCollectV];
    [flowLayout release];
    [self.goodListCollectV release];
    
    self.goodListCollectV.delegate = self;
    self.goodListCollectV.dataSource = self;
    
    [self.goodListCollectV registerClass:[GoodListCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"loading";
    [self.HUD show:YES];
    
    self.goodListArray = [NSMutableArray array];
    [self.goodListArray removeAllObjects];
    self.page = 1;
    [self.goodListCollectV addLegendFooterWithRefreshingBlock:^{
        self.page++;

        if (self.page == 11) {
            
            [self.goodListCollectV.footer noticeNoMoreData];
        } else {
                [self downLoadData];
                [self.goodListCollectV.footer endRefreshing];
        }
    }];
    
    [self downLoadData];
    // Do any additional setup after loading the view.
}

-(void)downLoadData{
    
    NSString *getUrl = @"";
    
    if (self.isSort == YES) {
        
        getUrl = [NSString stringWithFormat:@"http://121.41.84.91:80/HandheldKitchen/api/home/tblAssort!getVegetable.do?id=%ld&page=%ld&pageRecord=10&is_traditional=0&phonetype=1",self.typeId, self.page];
//        NSLog(@"%ld-----",self.typeId);
    } else {
        
        getUrl = [NSString stringWithFormat:@"http://121.41.84.91:80/HandheldKitchen/api/found/tblBoutique!getTblBoutiqueVegetableList.do?is_traditional=0&page=%ld&pageRecord=10&phonetype=0&user_id=0&typeId=%ld",self.page, self.typeId];
    }
    
    [AFNetWorkingYU netWorkingHanderGetGataWithUrl:getUrl resultBlock:^(id result) {
        
//        NSLog(@"----%@------",result);
        
        NSMutableArray *array = [result objectForKey:@"data"];
        for (NSMutableDictionary *dic in array) {
            
            GoodListModel *goodList = [[GoodListModel alloc] initWithDic:dic];
            [self.goodListArray addObject:goodList];
            [goodList release];
        }
        [self.goodListCollectV reloadData];
        [self.HUD hide:YES];
    }];
    
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.goodListArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    GoodListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    
    cell.goodList = [self.goodListArray objectAtIndex:indexPath.item];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CookingViewController *cookingVC = [[CookingViewController alloc] init];
    GoodListModel *goodList = [self.goodListArray objectAtIndex:indexPath.item];
    cookingVC.vegetable_id = goodList.vegetable_id;
    cookingVC.foodName = goodList.name;
    cookingVC.imagePathThumbnails = goodList.imagePathThumbnails;
    [self.navigationController pushViewController:cookingVC animated:YES];
    [cookingVC release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
